﻿package edu.pku.se.chocolate;

import java.util.Stack;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import edu.pku.se.chocolate.UI.View.ChocolateView;
import edu.pku.se.chocolate.UI.View.FavoritesView;
import edu.pku.se.chocolate.UI.View.InformationView;
import edu.pku.se.chocolate.UI.View.MainSettingsView;
import edu.pku.se.chocolate.UI.View.MainView;
import edu.pku.se.chocolate.solver.ChocolateSolver;

public class ChocolateActivity extends Activity {
    /** Called when the activity is first created. */
	String s = "lixiang";
	String jason = "江海挺JiangHaiting@gmail.com";
	String lyh = "liyouhuan";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welecome);
        new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				ChocolateActivity.this.getWindowManager().getDefaultDisplay().getMetrics(DM);
		        //建立界面栈
		        view_stack = new Stack<ChocolateView>();
		        ChocolateSolver.InitChocolateSolver(ChocolateActivity.this);
		        c_solver = ChocolateSolver.getInstance();
		        view_stack.clear();
		        //所有界面使用1个Activity
		        ChocolateView.setActivity(ChocolateActivity.this);
		        inflate = (LayoutInflater)ChocolateActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		        //设置主界面对象
		        mview = new MainView(inflate.inflate(R.layout.main, null));
		        mview.setListener();
		        msv = new MainSettingsView(mview,inflate.inflate(R.layout.mainsettings, null));
		        mview.setMainSettingsView(msv);
//		        mview.show();
		        fv = new FavoritesView(inflate.inflate(R.layout.favorites, null));
				mview.show();
			}
        	
        }, 2000);
    }
    
    public void onBackPressed() {
    	ChocolateView temp  = null;
    	if(!view_stack.isEmpty()) {
    		temp = view_stack.pop();
    	}
    	else {
    		Builder dlg = new AlertDialog.Builder(ChocolateActivity.this);
    		dlg.setTitle("确定退出？");
    		dlg.setPositiveButton("确定",new DialogInterface.OnClickListener() {

    			public void onClick(DialogInterface dialog, int which) {
    				finish();
    			}
    		});
    		dlg.setNegativeButton("取消", new DialogInterface.OnClickListener()   {
    			public void onClick(DialogInterface dialog, int whichButton)
    			{
    				;
    			}
    		});
    		dlg.create();//创建
    		dlg.show();//显示	
    	}
    	if(!view_stack.isEmpty())
    		view_stack.pop().show();
    	else {
    		Builder dlg = new AlertDialog.Builder(ChocolateActivity.this);
            dlg.setTitle("确定退出？");
            dlg.setPositiveButton("确定",new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
            final ChocolateView ttemp  = temp;
            dlg.setNegativeButton("取消", new DialogInterface.OnClickListener()   {
	                public void onClick(DialogInterface dialog, int whichButton)
	                {
	                	if(ttemp!=null)
	                	view_stack.push(ttemp);
	                }
	            });
            dlg.create();//创建
            dlg.show();//显示	
    	}
    }

    /**
     * 初始化建立菜单
     */
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		menu.setGroupVisible(R.id.group1, false);
		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		if(view_stack.isEmpty()) {
			menu.setGroupVisible(R.id.group1, false);
		}
		else {
			menu.setGroupVisible(R.id.group1, view_stack.peek().deleteVisible);
		}
		return true;
		
	}
	 
	/*处理菜单事件*/
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
		int item_id=item.getItemId();//得到当前选中MenuItem的ID
		switch(item_id){
			case R.id.item1:{
//				setMainView();
				mview.show();
				break;
			}
			case R.id.item2:{
//				setFavoriteView();
				fv.show();
				break;
			}
			case R.id.item3:{
				msv.show();
				break;
			}
			default: {
				if(!view_stack.isEmpty()) {
					final InformationView recentView = (InformationView) view_stack.pop();
					if(recentView.getTagRow().select == 0 && !recentView.isCustom()) {
						Toast.makeText(ChocolateActivity.this, "无法操作", Toast.LENGTH_SHORT).show();
						view_stack.push(recentView);
					}
					else {//删除选项功能
						
			            //创建对话框
						Builder dlg = new AlertDialog.Builder(ChocolateActivity.this);
			            dlg.setTitle("确定删除？");
			            dlg.setPositiveButton("确定",new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								recentView.deleteTag();
								recentView.show();
							}
						});
			            dlg.setNegativeButton("取消", new DialogInterface.OnClickListener()   {
				                public void onClick(DialogInterface dialog, int whichButton)
				                {
				                	recentView.show();
				                }
				            });
			            dlg.create();//创建
			            dlg.show();//显示	
					}
				}
			}
		}
		return true;
    }
    
    public static LayoutInflater inflate;
    private MainView mview;
    private MainSettingsView msv;
    private FavoritesView fv;
    public static Stack<ChocolateView> view_stack;
    public static ChocolateSolver c_solver;
    public static DisplayMetrics DM =  new DisplayMetrics();
}